﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for PageBase
/// </summary>
public class PageBase:System.Web.UI.Page
{
    public PageBase()
    {
        try
        {
            CheckIsLogin();
        }
        catch
        {
            Common.ParentRedirect("" + ConfigurationManager.AppSettings["CONST_DOMAIN"].ToString() + "login.aspx");
        }
    }

    private string _account;
    private int _uid;
    private int _type;

    private string _headImg;


    public string Account
    {
        get
        {
            if (_account == null || _account == string.Empty)
            {
                CheckIsLogin();
            }

            return _account;
        }
    }

    public int Uid
    {
        get
        {
            if (_uid == 0)
            {
                CheckIsLogin();
            }

            return _uid;
        }
    }


    public int userType
    {
        get
        {
            if (_type == 0)
            {
                CheckIsLogin();
            }

            return _type;
        }
    }

    //public string headImg
    //{
    //    get {
    //        if (string.IsNullOrEmpty(_headImg))
    //        {
    //            CheckIsLogin();
    //        }
    //        else
    //        {
    //            _headImg = "015bead7-e4f1-4832-bd64-1ad9bb72a53a.jpg";  //默认头像
    //        }
    //        return _headImg;
    //    }
    //}

    public virtual void CheckIsLogin()
    {
        HttpCookie userCookie = HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["COOKIE_KEY_USER"] as string];
        try
        {
            if (userCookie != null)
            {
                //DataTable dtUser = DataHandler.Current.GetUserByidSetCookie(Convert.ToInt32(PageHelper.Current.GetCookie()[0]));
                //if (dtUser != null && dtUser.Rows.Count > 0)
                //{
                //    _id = Convert.ToInt32(dtUser.Rows[0][0]);
                //    _account = dtUser.Rows[0][1].ToString();
                //    _name = dtUser.Rows[0][2].ToString();

                //}

                _uid = Convert.ToInt32(userCookie.Values["id"]);
                _account = userCookie.Values["name"].ToString();
                _type = Convert.ToInt32(userCookie.Values["type"].ToString());
                _headImg = userCookie.Values["headImg"].ToString();
            }
            else
            {
                HttpContext.Current.Response.Redirect("~/Account/login.aspx");
            }
        }
        catch (Exception ex)
        {
            //Logger.WriteLog(ex.Message);
            HttpContext.Current.Response.Redirect("~/Account/login.aspx");
        }
    }

}